package com.bnz.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bnz.common.entity.R;
import com.bnz.shop.entity.SpecificationOptionEntity;
import com.bnz.shop.service.SpecificationOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.shop.entity.SpecificationEntity;
import com.bnz.shop.service.SpecificationService;
import com.bnz.common.utils.PageUtils;




/**
 * 
 *
 * @author Haziy
 * @email 1143878025@qq.com
 * @date 2023-02-16 14:12:33
 */
@RestController
@RequestMapping("shop/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @Autowired
    private SpecificationOptionService optionService;
    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/findOptionsBySpecId")
    public R findOptionsBySpecId(@RequestBody List<Map> list){
        for (Map map : list) {
            //1. 得到规格id
            String id = map.get("id").toString();
            //2. 根据规格id，查询规格选项
            List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
            //3. 为map添加属性
            map.put("options",options);
        }
        return R.ok().put("specs",list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		SpecificationEntity specification = specificationService.getById(id);

        return R.ok().put("specification", specification);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SpecificationEntity specification){
		specificationService.save(specification);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody SpecificationEntity specification){
		specificationService.updateById(specification);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids){
		specificationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
